package top.lixunda.ecommerce.server.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.BusinessConstants;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheLoginUser;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheStore;
import top.lixunda.ecommerce.server.user.api.entity.db.StaffDO;
import top.lixunda.ecommerce.server.user.api.entity.db.UserDetailDO;
import top.lixunda.ecommerce.server.user.api.entity.db.UserInfoDO;
import top.lixunda.ecommerce.server.user.mapper.StaffMapper;
import top.lixunda.ecommerce.server.user.mapper.UserDetailMapper;
import top.lixunda.ecommerce.server.user.mapper.UserInfoMapper;
import top.lixunda.ecommerce.server.user.service.IUserInfoService;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户的基本信息，如qq，微信，邮箱等 服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2019-11-19
 */
@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoMapper, UserInfoDO>
        implements IUserInfoService {

    private final StaffMapper staffMapper;

    private final UserDetailMapper userDetailMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public UserInfoServiceImpl(StaffMapper staffMapper,
                               UserDetailMapper userDetailMapper) {
        this.staffMapper = staffMapper;
        this.userDetailMapper = userDetailMapper;
    }

    /**
     * 根据用户类型查询人员基本信息，发送消息时使用
     *
     * @param type 用户类型
     * @return 用户信息列表
     */
    @Override
    public List<UserInfoDO> queryUserByType(String type) {
        if (ObjectValidators.isEmpty(type)) {
            throw getAppException("查询人员信息失败！用户类型不能为空！");
        }
        List<StaffDO> staffList;
        List<Integer> accountIdList;
        switch (type) {
            case BusinessConstants.USER_TYPE_STORE:
                CacheStore cacheStore = currentStoreCache();
                if (ObjectValidators.isEmpty(cacheStore)) {
                    throw getAppException("权限不足！");
                }
                // 获取员工信息
                staffList = staffMapper.selectList(
                        new LambdaQueryWrapper<StaffDO>()
                                .eq(StaffDO::getUserType, BusinessConstants.USER_TYPE_STORE)
                                .eq(StaffDO::getSystemType, BusinessConstants.SYSTEM_TYPE_STORE)
                                .eq(StaffDO::getSystemId, cacheStore.getId()));
                // 获取人员信息
                accountIdList = staffList.stream()
                        .map(StaffDO::getUserId)
                        .collect(Collectors.toList());
                break;
            case BusinessConstants.USER_TYPE_ADMIN:
                // 获取员工信息
                staffList = staffMapper.selectList(
                        new LambdaQueryWrapper<StaffDO>()
                                .eq(StaffDO::getUserType, BusinessConstants.USER_TYPE_ADMIN)
                                .eq(StaffDO::getSystemType, BusinessConstants.SYSTEM_TYPE_ADMIN));
                // 获取人员信息
                accountIdList = staffList.stream()
                        .map(StaffDO::getUserId)
                        .collect(Collectors.toList());
                break;
            default:
                throw getAppException("查询人员信息失败！不知道的用户类型参数！");
        }
        if (ObjectValidators.isNotEmpty(accountIdList)) {
            List<UserInfoDO> userInfoList = (List<UserInfoDO>) this.listByIds(accountIdList);
            // todo 测试，不排除掉自己
            CacheLoginUser cacheLoginUser = currentLoginUserCacheNotNull();
//            userInfoList = userInfoList.stream()
//                    .filter(item -> !item.getAccountId().equals(cacheLoginUser.getAccount()))
//                    .collect(Collectors.toList());
            return userInfoList;
        }
        return Collections.emptyList();
    }

    /**
     * 查询当前登陆账户详细信息
     *
     * @return 用户详细信息
     */
    @Override
    public UserDetailDO queryCurrentUserDetail() {
        CacheLoginUser cacheLoginUser = currentLoginUserCache();
        if (ObjectValidators.isEmpty(cacheLoginUser)) {
            return null;
        }
        List<UserDetailDO> list = userDetailMapper.selectList(new LambdaQueryWrapper<UserDetailDO>()
                .eq(UserDetailDO::getAccount, cacheLoginUser.getAccount())
                .eq(UserDetailDO::getUserType, cacheLoginUser.getType()));
        if (ObjectValidators.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
}
